//
//  TXPLoginPageViewController.m
//  StayAwhile
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TXPLoginPageViewController.h"
#import "TXPLandPageViewController.h"

@interface TXPLoginPageViewController ()
@property (nonatomic, retain) UIImageView *headImageView;
@property (nonatomic, retain) UIButton *boyBtn;
@property (nonatomic, retain) UIButton *girlBtn;
@property (nonatomic, retain) UITextField *userTextField;
@property (nonatomic, retain) UITextField *emialTextField;
@property (nonatomic, retain) UITextField *keyTextField;
@property (nonatomic, retain) UIButton * completeButton;

@end

@implementation TXPLoginPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBA(245, 245, 245, 1);
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnButton setImage:[UIImage imageNamed:@"iconfont-fanhui-6.png"] forState:UIControlStateNormal];
    [self.view addSubview:returnButton];
    [returnButton addTarget:self action:@selector(returnToLandPage:) forControlEvents:UIControlEventTouchUpInside];
    [returnButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_left).offset(20);
        make.top.mas_equalTo (self.view.mas_top).offset(20);
        make.size.mas_equalTo(CGSizeMake(45, 40));
        
    }];
    
    self.headImageView =[[ UIImageView alloc] init];
    self.headImageView.image = [UIImage imageNamed:@"iconfont-touxiang-4.png"];
    [self.view addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_left).offset((WIDTH - 100) / 2);
        make.top.mas_equalTo (self.view.mas_top).offset(100);
        make.size.mas_equalTo(CGSizeMake(100, 100));
        
    }];

    self.boyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.boyBtn.tag = 1001;
    self.boyBtn.backgroundColor = [UIColor lightGrayColor];
    self.boyBtn.layer.cornerRadius = 17;
    self.boyBtn.layer.masksToBounds = YES;
    [self.boyBtn addTarget:self action:@selector(clickBtnChangeColor:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.boyBtn];
    [self.boyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_left).offset((WIDTH - 200) / 2);
        make.top.mas_equalTo (self.view.mas_top).offset(210);
        make.size.mas_equalTo(CGSizeMake(70, 35));
        
    }];
    
    UIImageView *boyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 6, 20, 20)];
    boyImageView.image = [UIImage imageNamed:@"iconfont-nan-2.png"];
    [self.boyBtn addSubview:boyImageView];
    
    UILabel *boyLabel = [[UILabel alloc] initWithFrame:CGRectMake(33, 6, 20, 20)];
    boyLabel.text = @"男";
    boyLabel.textColor = [UIColor whiteColor];
    [self.boyBtn addSubview:boyLabel];

    
    self.girlBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.girlBtn.backgroundColor = [UIColor lightGrayColor];
    self.girlBtn.layer.cornerRadius = 17;
    self.girlBtn.layer.masksToBounds = YES;
    self.girlBtn.tag = 1002;
    [self.girlBtn addTarget:self action:@selector(clickBtnChangeColor:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.girlBtn];
    [self.girlBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.boyBtn.mas_right).offset(60);
        make.top.mas_equalTo (self.view.mas_top).offset(210);
        make.size.mas_equalTo(CGSizeMake(70, 35));
        
    }];

    UIImageView *girlImageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 6, 22, 22)];
    girlImageView.image = [UIImage imageNamed:@"iconfont-nv.png"];
    [self.girlBtn addSubview:girlImageView];
    
    UILabel *girlLabel = [[UILabel alloc] initWithFrame:CGRectMake(33, 6, 20, 20)];
    girlLabel.text = @"女";
    girlLabel.textColor = [UIColor whiteColor];
    [self.girlBtn addSubview:girlLabel];

    UILabel *userLabel = [[UILabel alloc] init];
    userLabel.text = @"昵称:";
    userLabel.font = [UIFont systemFontOfSize:19];
    [self.view addSubview:userLabel];
    [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        make.top.mas_equalTo (self.boyBtn.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(60, 40));
        
    }];
    
    UIView *lineView = [[UIView alloc] init];
    [self.view addSubview:lineView];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (userLabel.mas_bottom).offset(5);
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        
        make.size.mas_equalTo (CGSizeMake(375 - 120, 1));
        
    }];

    self.userTextField = [[UITextField alloc] init];
    [[UITextField appearance] setTintColor:[UIColor blackColor]];
    [self.view addSubview:self.userTextField];
    self.userTextField.borderStyle = UITextBorderStyleNone;
    [self.userTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (userLabel.mas_right).offset(0);
        make.top.mas_equalTo (self.boyBtn.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(375 - 120, 40));
        
    }];

    UILabel *emialLabel = [[UILabel alloc] init];
    emialLabel.text = @"邮箱:";
    emialLabel.font = [UIFont systemFontOfSize:19];
    [self.view addSubview:emialLabel];
    [emialLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        make.top.mas_equalTo (lineView.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(60, 40));
        
    }];
    
    UIView *twolineView = [[UIView alloc] init];
    [self.view addSubview:twolineView];
    twolineView.backgroundColor = [UIColor lightGrayColor];
    [twolineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (emialLabel.mas_bottom).offset(5);
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        
        make.size.mas_equalTo (CGSizeMake(375 - 120, 1));
        
    }];
    
    self.emialTextField = [[UITextField alloc] init];
    [[UITextField appearance] setTintColor:[UIColor blackColor]];
    [self.view addSubview:self.emialTextField];
    self.emialTextField.borderStyle = UITextBorderStyleNone;
    [self.emialTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (emialLabel.mas_right).offset(0);
        make.top.mas_equalTo (lineView.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(375 - 120, 40));
        
    }];


    UILabel *keyLabel = [[UILabel alloc] init];
    keyLabel.text = @"密码:";
    keyLabel.font = [UIFont systemFontOfSize:19];
    [self.view addSubview:keyLabel];
    [keyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        make.top.mas_equalTo (twolineView.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(60, 40));
        
    }];
    
    UIView *threelineView = [[UIView alloc] init];
    [self.view addSubview:threelineView];
    threelineView.backgroundColor = [UIColor lightGrayColor];
    [threelineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (keyLabel.mas_bottom).offset(5);
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        
        make.size.mas_equalTo (CGSizeMake(375 - 120, 1));
        
    }];
    
    self.keyTextField = [[UITextField alloc] init];
    [[UITextField appearance] setTintColor:[UIColor blackColor]];
    self.keyTextField.secureTextEntry = YES;
    [self.view addSubview:self.keyTextField];
    self.keyTextField.borderStyle = UITextBorderStyleNone;
    [self.keyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (keyLabel.mas_right).offset(0);
        make.top.mas_equalTo (twolineView.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(375 - 120, 40));
        
    }];
    
    self.completeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.completeButton.backgroundColor = RGBA(119, 183, 69, 1);
    [self.completeButton setTitle:@"完成" forState:UIControlStateNormal];
    self.completeButton.tintColor = RGBA(119, 183, 69, 1);
    [self.completeButton addTarget:self action:@selector(clickCompleteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.completeButton];
    [self.completeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (threelineView.mas_bottom).offset(40);
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        
        make.size.mas_equalTo (CGSizeMake(WIDTH - 120, 50));
        
    }];


}
- (void)clickBtnChangeColor:(UIButton *)btn{
    if (btn.tag == 1001) {
        btn.backgroundColor = RGBA(69, 125, 213, 1);
        UIButton *button = [self.view viewWithTag:1002];
        button.backgroundColor = [UIColor lightGrayColor];
    } else{
        
        btn.backgroundColor = RGBA(227, 49, 57, 1);
        UIButton *button = [self.view viewWithTag:1001];
        button.backgroundColor = [UIColor lightGrayColor];
    }
    
    
    }

- (void)clickCompleteButtonAction:(UIButton *)btn{
    if (self.userTextField.text.length == 0 || self.keyTextField.text.length == 0 || self.emialTextField.text.length == 0) {
        MBProgressHUD *promptHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        promptHUD.mode = MBProgressHUDModeText;
        promptHUD.labelText = @"请检查您的邮箱格式";
        promptHUD.labelColor = [UIColor whiteColor];
        [promptHUD hide:YES afterDelay:0.5];
    
    
    }
    else {
        
        BmobUser *bUser = [[BmobUser alloc] init];
        [bUser setUsername:self.userTextField.text];
        [bUser setPassword:self.keyTextField.text];
        [bUser setEmail:self.emialTextField.text];
        [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                NSLog(@"sign up successfully");
                [self.navigationController popToRootViewControllerAnimated:YES];
                
                
            } else {
                NSLog(@"%@", error);
                NSString *errorInfo = [error.userInfo objectForKey:error];
                
                if ([errorInfo rangeOfString:@"username"].location != NSNotFound) {
                    MBProgressHUD *promptHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                    promptHUD.mode = MBProgressHUDModeText;
                    promptHUD.labelText = @"昵称已经被占用了";
                    promptHUD.labelColor = [UIColor whiteColor];
                    [promptHUD hide:YES afterDelay:0.5];
                } if([errorInfo rangeOfString:@"email"].location != NSNotFound) {
                    MBProgressHUD *promptHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                    promptHUD.mode = MBProgressHUDModeText;
                    promptHUD.labelText = @"该邮箱已被注册";
                    promptHUD.labelColor = [UIColor whiteColor];
                    [promptHUD hide:YES afterDelay:0.5];
                    
                }
                
                
        }
            
        }];
        
    }


}
- (void)returnToLandPage:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
